package com.tjn.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 陶镓妮
 * @version 1.0
 */
public class ArraysSort {
    public static void main(String[] args) {
        int[] arr = {10, -1, 20, 5, 7, 90};
        //bubble(arr);

        bubble01(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i1 - i2;
                //return i2 - i1;
            }
        });

        System.out.println("===定制排序后===");
        System.out.println(Arrays.toString(arr));
    }

    //冒泡排序
    public static void bubble(int[] arr){
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length - 1 - i; j++){
                //从小到大
                if(arr[j] > arr[j + 1]){
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    //结合冒泡定制排序
    public static void bubble01(int[] arr, Comparator c){
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr.length - 1 - i; j++){
                //从小到大
                if(c.compare(arr[j], arr[j + 1]) > 0){//排序方式由这里决定
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
